
-- Localize globals
local SERVER = SERVER 
local CLIENT = CLIENT 
local ConVarExists = ConVarExists 
local GetConVar = GetConVar 
local CreateClientConVar = CreateClientConVar 
local CreateConVar = CreateConVar 
local tostring = tostring 
local tobool = tobool 
local os = os 

-- Crate the shared folder
if not admin then 
	admin = {}
end

--[[ 
	ConVar and ConCommand Handling
		-- admin.addConVar( strName, numStart, tblFlags, strHelp )
		-- admin.addConcommand - not done yet
]]-- 


-- We want to keep track of the convars.
function admin.addConVar( string, startVal, tblFlags, help )
	local string = "jb_"..string
	if not ConVarExists( string ) then
		if CLIENT then 
			admin.ccvars[ string ] = CreateClientConVar( string, startVal, tblFlags, help )
		elseif SERVER then
			admin.cvars[ string ] = CreateConVar( string, startVal, tblFlags, help )
		end
	else
		if CLIENT then
			admin.ccvars[ string ] = GetConVar( string )
		elseif SERVER then
			admin.cvars[ string ] = GetConVar( string )
		end
	end
end

---------------------------------------------------------------

--[[ 
	Time/Date functions
		-- admin.toDate( time )        -- Converts Unix Epoch to nicely formatted date.
		-- admin.currTimeBan( num )    -- Used for calculating ban times.
		-- admin.timeUntilUnban( num ) -- Used for calculating unban times.
]]-- 

-- Function to format Unix Epoch times to legible dates
function admin.toDate( time )
	return tostring( os.date( "%I:%M %p - %m/%d/%y", time ) )
end

-- For adding ban lengths ( minutes, days, etc. )
function admin.currTimeBan( number )
	return CurTime() + number
end

-- For calculating unban times
function admin.timeUntilUnban( number )
	return number - CurTime()
end




---------------------------------------------------------

--[[
	Find helpers
		-- admin.findPlayerByName( name )
]]--

-- Simple find function
function admin.findPlayerByName( name )
	local ply = nil
	local all = player.GetAll()

	for i=1,#all do
		if string.find( all[i]:Name(), name ) then
			ply = all[i]
		end
	end

	return ply 
end

--[[
	Rank functions
		-- ply:Rank()              returns: string rank
		-- ply:IsJBPotato()        returns: bool ispotato
		-- ply:IsJBRespected()     returns: bool isrespected
		-- ply:IsJBDonor()         returns: bool is donor
		-- ply:IsJBAdmin()         returns: bool isadmin
		-- ply:IsJBSuperAdmin()    returns: bool issuperadmin
		-- ply:IsJBOwner()         returns: bool isowner
		-- admin.getRankValue( rank ) returns: num rankval
]]--

local plymeta = FindMetaTable( "Player" )

function plymeta:Rank() 
	return self.rank
end

function plymeta:IsJBPotato()
	return tobool( self.rank == "potato" )
end

function plymeta:IsJBRespected()
	return tobool( self.rank == "respected" )
end

function plymeta:IsJBDonor()
	return tobool( self.rank == "donor" )
end

function plymeta:IsJBAdmin()
	return tobool( self.rank == "admin" ) or tobool( self.rank == "superadmin" )
end

function plymeta:IsJBSuperAdmin()
	return tobool( self.rank == "superadmin" )
end

function plymeta:IsJBOwner()
	return tobool( self.rank == "owner" )
end

function admin.getRankValue( rank )
	if rank == "potato" then return 0 end
	if rank == "user" then return 1 end
	if rank == "respected" then return 2 end
	if rank == "donor" then return 3 end
	if rank == "admin" then return 4 end
	if rank == "superadmin" then return 5 end
	if rank == "owner" then return 6 end
end


--------------------------------------------------------------------

--[[
	Miscellaneous Helpers
		-- admin.vecToColor( vecVector )
		-- vec:ToColor() 
		-- admin.stringToColor( strString )
		-- admin.colorToVec( colColor )
]]--

function admin.vecToColor( vec )
	if vec.a then
		return Color( vec.x, vec.y, vec.z, vec.a )
	end
	
	return Color( vec.x, vec.y, vec.z )
end

local vecmeta = FindMetaTable( "Vector" )

function vecmeta:ToColor()
	return admin.vecToColor( self )
end


function admin.stringToColor( str )
	return util.StringToType( str, "Vector" ):ToColor()
end


function admin.colorToVec( col )
	return Vector( col.r, col.g, col.b, col.a )
end


